package com.pengpeng.server.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.pengpeng.server.database.Database;
import com.pengpeng.server.database.DatabaseFactory;
import com.pengpeng.server.database.ISqlObj;
import com.pengpeng.server.entity.PengPengMessage;

public class ResponseHistoryMessageDao{
	
	private DatabaseFactory databasefactory;
	
    public Map<String,PengPengMessage> queryByUserphonenum(String userphonenum)
    {
    	RowMapper<PengPengMessage> mapper=new RowMapper<PengPengMessage>()
		{
			@Override
			public PengPengMessage mapRow(ResultSet rs, int arg1) throws SQLException 
			{
				
				String userphonenum;
				String messagetype;
				String messageid;
				String messagetext;
				String target;
				String ifneedconfirm;
				String messagetimestr;
				Date   messagetime;
				
				userphonenum=rs.getString("userphonenum");
				messagetype=rs.getString("messagetype");
				messageid=rs.getString("messageid");
				messagetext=rs.getString("messagetext");
				target=rs.getString("target");
				ifneedconfirm=rs.getString("ifneedconfirm");
				messagetimestr=rs.getString("responsetime");
		
				SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					messagetime= dateformat.parse(messagetimestr);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					messagetime=new Date();
				}
					
				PengPengMessage ppmessage=new PengPengMessage();
				ppmessage.setUserphonenum(userphonenum);
				ppmessage.setMessagetype(messagetype);
				ppmessage.setMessageid(messageid);
				ppmessage.setMessagetext(messagetext);
				ppmessage.setTarget(target);
				ppmessage.setIfneedconfirm(ifneedconfirm);
				ppmessage.setMessagetime(messagetime);
				
				return ppmessage;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryResponseHistoryMessageByUserphonenum");
		final Object[] params = new Object[]{userphonenum};
		List<PengPengMessage> ppmesagelist;
	    try {  
	    	ppmesagelist=jdbctemplate.query(sqlstr, params,mapper);
		   
	        } catch (EmptyResultDataAccessException e) {  
	          ppmesagelist=null;  
	    } 
	    if(null!=ppmesagelist)
	    {
	    	Map<String,PengPengMessage> ppmesagemap=new HashMap<String,PengPengMessage>();
	    	ppmesagemap=Collections.synchronizedMap(ppmesagemap);
	    	PengPengMessage ppmessage;
		    
	    	for(int i=0;i<ppmesagelist.size();i++)
	    	{
	    		ppmessage=ppmesagelist.get(i);
	    		ppmesagemap.put(ppmessage.getMessageid(), ppmessage);
	    	}
	    	return  ppmesagemap; 
	    }// if(null!=ppmesagelist)
	    else
	    {
	    	return null;
	    }	  //else      
	    	
    }//queryByUserphonenum
    
    
    public PengPengMessage queryByKey(String userphonenum,String messageid)
    {
    	RowMapper<PengPengMessage> mapper=new RowMapper<PengPengMessage>()
		{
			@Override
			public PengPengMessage mapRow(ResultSet rs, int arg1) throws SQLException 
			{
				
				String userphonenum;
				String messagetype;
				String messageid;
				String messagetext;
				String target;
				String ifneedconfirm;
				String messagetimestr;
				Date   messagetime;
				
				userphonenum=rs.getString("userphonenum");
				messagetype=rs.getString("messagetype");
				messageid=rs.getString("messageid");
				messagetext=rs.getString("messagetext");
				target=rs.getString("target");
				ifneedconfirm=rs.getString("ifneedconfirm");
				messagetimestr=rs.getString("responsetime");
		
				SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					messagetime= dateformat.parse(messagetimestr);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					messagetime=new Date();
				}
					
				PengPengMessage ppmessage=new PengPengMessage();
				ppmessage.setUserphonenum(userphonenum);
				ppmessage.setMessagetype(messagetype);
				ppmessage.setMessageid(messageid);
				ppmessage.setMessagetext(messagetext);
				ppmessage.setTarget(target);
				ppmessage.setIfneedconfirm(ifneedconfirm);
				ppmessage.setMessagetime(messagetime);
				
				return ppmessage;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryResponseHistoryMessageByKey");
		final Object[] params = new Object[]{userphonenum,messageid};
	    try {  
	    	return jdbctemplate.queryForObject(sqlstr, params,mapper);
		   
	        } catch (EmptyResultDataAccessException e) {  
	          return null;  
	    } 
    }//queryByKey
    
    public boolean insert(PengPengMessage ppmessage)
    {

    	Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String userphonenum=ppmessage.getUserphonenum();
		String messageid=ppmessage.getMessageid();
		String messagetype=ppmessage.getMessagetype();
		String messagetext=ppmessage.getMessagetext();
		String target=ppmessage.getTarget();
		String ifneedconfirm=ppmessage.getIfneedconfirm();
		String ifconfirmed="1";

		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String messagetimestr=dateformat.format(ppmessage.getMessagetime());
	
		Object[] params = new Object[] {userphonenum,messageid,messagetype,messagetext,target,ifneedconfirm,ifconfirmed,messagetimestr};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("insertResponseHistoryMessage");
		jdbctemplate.update(sqlstr,params);		
		return true;
    }//insert
    public boolean update(PengPengMessage ppmessage)
    {
    	Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String userphonenum=ppmessage.getUserphonenum();
		String messageid=ppmessage.getMessageid();
		String messagetype=ppmessage.getMessagetype();
		String messagetext=ppmessage.getMessagetext();
		String target=ppmessage.getTarget();
		String ifneedconfirm=ppmessage.getIfneedconfirm();

		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String messagetimestr=dateformat.format(ppmessage.getMessagetime());
	
		Object[] params = new Object[] {userphonenum,messageid,messagetype,messagetext,target,ifneedconfirm,messagetimestr,userphonenum,messageid,};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("updateResponseHistoryMessage");
		jdbctemplate.update(sqlstr,params);		
		return true;
    }//update
    public boolean deleteByUserphonenum(String userphonenum)
    {
    	Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		Object[] params = new Object[] {userphonenum};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("deleteResponseHistoryMessageByUserphonenum");
		jdbctemplate.update(sqlstr,params);
		
		return true;
    }//deleteByUserphonenum
    public boolean deleteByKey(String userphonenum,String messageid)
    {
    	Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		Object[] params = new Object[] {userphonenum,messageid};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("deleteResponseHistoryMessageByKey");
		jdbctemplate.update(sqlstr,params);	
		return true;
    }//deleteByKey
    public boolean copyaFromRealResponseMessages(String userphonenum)
    {   
    	Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		Object[] params = new Object[] {userphonenum};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("copyRealtoHistoryResponseMessages");
		jdbctemplate.update(sqlstr,params);			
    	return true;
    }//copyaFromRealResponseMessages
    
    public boolean copyaFromRealResponseMessage(String userphonenum,String messageid)
    {   
    	Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		Object[] params = new Object[] {userphonenum,messageid};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("copyRealtoHistoryResponseMessage");
		jdbctemplate.update(sqlstr,params);	
		
    	return true;
    }//copyaFromRealResponseMessages


	public DatabaseFactory getDatabasefactory() {
		return databasefactory;
	}


	public void setDatabasefactory(DatabaseFactory databasefactory) {
		this.databasefactory = databasefactory;
	}
}
